require([
  'jquery',
  'pageInit',
  'dataTableFilter',
  'loading',
  'bootstrap-dialog',
  'notification',
  'listAsyncOp'
], function($, vamakerPage, VamDataTable, VamLoader, BootstrapDialog, VamOpMsg, VamListOp) {
  $(function() {
    var 
      $dataTable = $('#data-table'),
      dataUrl = VAM.getVar('USERS_DATA_LIST'),
      dataEditUrl = VAM.getVar('DATA_EDIT_URL'),
      dataResetOp = VAM.getVar('DATA_RESET_PWD_OP_URL'),
      $resetPwdModal = $('#reset-pwd-modal'),
      $resetComfirmBtn = $('#reset-confirm-operate'),
      $reportDataThead = $dataTable.find('thead'),
      $loadingWrap = new VamLoader().loader($('#list-loading-wrap')),
    listTable, tableOptions = {};
    // Table settings.
    tableOptions = {
      'ajax': {
        'url': dataUrl
      },
      'order': [6,'desc'],
      'columns': [
        {'data': 'id'},
        {'data': 'name'},
        {'data': 'type'},
        {'data': 'email'},
        // {'data': 'role'},
        {'data': 'resource'},
        {'data': 'status'},
        {'data': 'lastModifiedDate'},
        {'data': 'operate'}
      ],
      'columnDefs': [
        {
          'targets': [0,1,2,3,4,6],
          'render': function (data, type, full, meta) {
            return '<p title="' + data + '">' + data +'</p>'
          }
        },
        {
          'targets': -3,
          'render': function (data, type, full, meta) {
            var result = data;
            /*
            if (data === 'to-be-audit') {
              result = '待审核';
            } else if (data === 'auditing') {
              result = '审核中';
            } else if (data === 'passed') {
              result = '审核通过';
            } else if (data === 'refused') {
              result = '审核不通过';
            };
            */
            return '<p title="' + result + '">' + result +'</p>';
          }
        },
        {
          'targets': -1,
          'render': function (data, type, full, meta) {
            return '<div class="tab-btn-group">'
                  + '<input class="row-info" type="hidden" value="' + full.id + '">'
                  + '<a href="' + dataEditUrl + '?id=' + full.id + '" class="btn btn-primary btn-xs tab-edit" data-type="edit" title="' + VAM.j('Edit') + '"><i class="fa fa-edit"></i></a>'
                  // + '<button class="btn btn-info btn-xs tab-report" data-loading-text="加载中..." title="报表"><i class="fa fa-file-text"></i></button>'
                  + '<button class="btn btn-danger btn-xs tab-reset" data-type="reset" data-loading-text="<i class=\'fa fa-spinner fa-spin\'></i>" title="' + VAM.j('Reset Password') + '"><i class="fa fa-key"></i></button>'
                  + '</div>';
          }
        }
      ],
      'initComplete': function() {
        tableResize = new VamDataTable().columnResize($dataTable);
      }
    };
    $dataTable
    .on('preXhr.dt', function(evt) {
      $loadingWrap.show();
    });
    listTable = new VamDataTable().dataTables($dataTable, tableOptions);
    // After table data list.
    listTable.on('xhr.dt', function(evt) {
      $loadingWrap.fadeOut();
    });

    // Reset password.
    $dataTable.on('click', 'tbody td .tab-reset', function(evt) {
      var $me = $(this),
      $rowInfo = $me.siblings('.row-info'),
      opId = $rowInfo.val(),
      opType = $me.data('type'),
      opData;
      opData = {
        user_id: opId,
        type: opType,
        rnd: (new Date()).getTime()
      };
      evt.preventDefault();
      BootstrapDialog.confirm({
        title: VAM.j('Reset Password'),
        message: VAM.j('Confirm reset this password?'),
        type: BootstrapDialog.TYPE_DANGER,
        closable: true, 
        draggable: true,
        btnCancelLabel: VAM.j('Cancel'), 
        btnOKLabel: VAM.j('OK'), 
        btnOKClass: 'btn-danger', 
        callback: function(result) {
          if(result) {
            VamListOp.listOp({
              url: dataResetOp,
              data: opData,
              before: function () {
                $me.button('loading');
              },
              success: function (data) {
                if (data.status === 'success') {
                  VamOpMsg.messager(data);
                  listTable.ajax.reload();
                } else {
                  VamOpMsg.messager(data);
                }
              },
              always: function () {
                $me.button('reset');
              }
            });
          }else {
          }
        }
      });
    });
  });
});
